
var express = require('express');
var router = express.Router();
const { login, data,submit } = require('../controllers/usersController');
const { uploadFiles, moveFiles, deleteFiles} = require('../util/handleFiles')
const path = require('path');



router.post('/login',login);
router.post('/data',data);
router.post('/submit',submit);
//图片
router.post('/uploadImage', function (req, res, next) {
    let path = 'temp';
     const uploadImages = uploadFiles({
         // 图片上传成功后的存储路径
         path: `./public/${path}`,
         key: 'file',
         size: 1024*10
     });
     uploadImages(req, res, (error) => {
         if(error) {
             console.log('图片上传失败', error);
             res.send({
                 code:0,
                 messges:'图片上传失败'
             })
         } else {
            console.log('图片上传成功');
             res.send({
                 code:200,
                 messges:'图片上传成功',
                 data:`${req.files[0].filename}`
             })
         }
     });
 });
 

 router.post('/del', function (req, res, next) {

    let str= req.body.imgSrc;
    let delPath = `../public/${str}`;
    let data=path.resolve(__dirname,delPath)
    console.log(data);
    // console.log(path.resolve(__dirname,delPath));
   const code= deleteFiles(data);
   console.log(code);
    res.send({code});
    // deleteImages

})

//确认添加
router.post('/addImage', function (req, res, next) {

    let addPath1 = `../public/temp`;
    let addPath2 = `../public/images`;
    let data1=path.resolve(__dirname,addPath1);
    let data2=path.resolve(__dirname,addPath2);
  let code=  moveFiles({
    fromPath:data1, 
    toPath:data2,
    filename: req.body.imagePath
    })
    res.send({code});

})


module.exports = router;